#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: syntax.sh
# Description: 
#   http://www.math.utah.edu/docs/info/gawk_1.html
#   http://www.grymoire.com/Unix/Awk.html#uh-4
#  syntax:
#    awk 'script' files   
#
#   files: a list of one or more files
#          If files, the list of files, is omitted, awk reads its input from STDIN.
#   script: one or more commands of the form:
#        /pattern/ { actions }
#        pattern: a regular expression
#        actions: one or more of the commands. If pattern is omitted, 
#                 awk performs the specified actions for each input line.

#   3.可以在BEGIN跟END之間,放許多/pattern/ { actions }
# 	awk '
# 	BEGIN { actions }
# 	/pattern/ { actions }
# 	  ...
# 	/pattern/ { actions }
# 	END { actions }
# 	' files
#   
#   Both the BEGIN and the END pattern are optional, so
#		When the BEGIN pattern is specified, awk executes its actions before reading any input.
#		When the END pattern is specified, awk executes its actions before it exits.
#
#

#   4.可以將shell變數給awk用
#   awk 'script' awkvar1=value awkvar2=value ... files

# Edit History: 
#   2010-12-23    File created.
#========================================================================

#use the awk command print to print each line of the input
awk '{ print ; }' fruit_prices.txt
#Notice that there is a semicolon ( ;) after the print command. This semicolon is required to let awk know
#that the command has concluded. Strictly speaking, some older versions of awk do not require this, but it is
#good practice to include it anyway
echo "===================================================="
#awk automatically divides input lines into fields. 
#A #field is a set of characters that are separated by one or more field separator characters. 
#The default field separator characters are tab and space.
awk '{ print $1 , $3 ; }' fruit_prices.txt


echo "===================================================="
#format the output by using the awk printf command instead of the print command
awk '{ printf "%-15s %s\n" , $1 , $3 ; }' fruit_prices.txt
echo "===================================================="
#The first one looks for fruit priced higher than a dollar, and the second one
#looks for fruit priced lower than a dollar.
#高於1快後面會加個*表示
#$0 is used by awk to store the entire input line as it was read
awk '
/ *\$[1-9][0-9]*\.[0-9][0-9] */ { print $0,"*"; }
/ *\$0\.[0-9][0-9] */ { print ; }
' fruit_prices.txt

#read from stdin
/bin/ls -l | awk '$1 !~ /total/ { printf "%-32s %s\n",$8,$5 ; }'
